package com.linkhub.oms.component.email.service.impl;

import com.linkhub.oms.common.constant.EmailConstant;
import com.linkhub.oms.component.email.service.LhMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static com.linkhub.oms.common.constant.ProjectConstant.ASYNC_EXECUTOR;

/**
 * @Description: LhMailService
 * @Author: echo
 * @Date: 2024/3/11 17:18
 * @Version: 1.0
 */
@Slf4j
@Service
public class LhMailServiceImpl implements LhMailService {

    @Resource
    private JavaMailSender javaMailSender;
    @Resource
    private MailProperties mailProperties;

    @Override
    @Async(ASYNC_EXECUTOR)
    public void sendGmailCode(String subject, String toEmail, String code) {

        //构建邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件主题
        message.setSubject(subject);
        message.setFrom(mailProperties.getUsername());
        //邮件接收者(可以有多个)
        message.setTo(toEmail);
        //邮件发送日期
        message.setSentDate(new Date());
        //邮件正文
        String content = String.format(EmailConstant.EMAIL_CODE_CONTENT, code);
        message.setText(content);

        javaMailSender.send(message);

        log.info("sendGmailCode {}", message);
    }
}
